/////////////////////////////////////////////////////////////////////////////
// Name:        actuator_dlg.cpp
// Purpose:     dialog for FGActuator
// Author:      Jon Berndt
// Created:     04/12/2007
// Copyright:   (c) Jon Berndt
// Licence:     GPL licence
//
// Functions:
//
// do_layout
// GetDataIn
// OnButtonPressCancel
// OnButtonPressHelp
// OnButtonPressOK
// OnCheckboxClip
// OnButtonPressCancel
// OnButtonPressHelp
// OnClickInvertInput
// SensorComponentEditor - Constructor
// set_properties
// SetDataOut
// Show
//
/////////////////////////////////////////////////////////////////////////////

// -*- C++ -*- generated by wxGlade 0.4.1 on Fri Apr 06 07:40:41 2007

#include "actuator_dlg.h"

// All non-MSW platforms use an xpm. MSW uses an .ico file
#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
    #include "actuator.xpm"
#endif

ActuatorComponentEditor::ActuatorComponentEditor(Actuator *actuator, wxWindow* parent, int id, const wxString& title, const wxPoint& pos, const wxSize& size, long style):
    wxDialog(parent, id, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxTHICK_FRAME|wxSTAY_ON_TOP|wxFULL_REPAINT_ON_RESIZE)
{
    // begin wxGlade: ActuatorComponentEditor::ActuatorComponentEditor
    nbk_Properties = new wxNotebook(this, -1, wxDefaultPosition, wxDefaultSize, wxRAISED_BORDER|wxTAB_TRAVERSAL|wxWS_EX_VALIDATE_RECURSIVELY);
    notebook_1_pane_1 = new wxPanel(nbk_Properties, -1);
    sizer_clip_staticbox = new wxStaticBox(notebook_1_pane_1, -1, wxT("Clipping"));
    sizer_actuator_properties_staticbox = new wxStaticBox(notebook_1_pane_1, -1, wxT("Actuator Properties"));
    sizer_inout_staticbox = new wxStaticBox(notebook_1_pane_1, -1, wxT("Input / Output"));
    sizer_basic_staticbox = new wxStaticBox(notebook_1_pane_1, -1, wxT("Basic Properties"));
    label_name = new wxStaticText(notebook_1_pane_1, -1, wxT("Name:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    txtEd_Name = new wxTextCtrl(notebook_1_pane_1, -1, wxT(""));
    label_type = new wxStaticText(notebook_1_pane_1, -1, wxT("Type:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    txtEd_Type = new wxTextCtrl(notebook_1_pane_1, -1, wxT("Actuator Component"), wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
    label_order = new wxStaticText(notebook_1_pane_1, -1, wxT("Order:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    txtEd_Order = new wxTextCtrl(notebook_1_pane_1, -1, wxT(""));
    chx_Clip = new wxCheckBox(notebook_1_pane_1, -1, wxT("Clip"));
    label_clip_min = new wxStaticText(notebook_1_pane_1, -1, wxT("Minimum:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    txtEd_MinClip = new wxTextCtrl(notebook_1_pane_1, -1, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
    label_clip_max = new wxStaticText(notebook_1_pane_1, -1, wxT("Maximum:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    txtEd_MaxClip = new wxTextCtrl(notebook_1_pane_1, -1, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
    label_7 = new wxStaticText(notebook_1_pane_1, -1, wxT("Lag"));
    txtEd_Lag = new wxTextCtrl(notebook_1_pane_1, -1, wxT(""));
    label_10 = new wxStaticText(notebook_1_pane_1, -1, wxT("Rate limit"));
    txtEd_RateLimit = new wxTextCtrl(notebook_1_pane_1, -1, wxT(""));
    label_11 = new wxStaticText(notebook_1_pane_1, -1, wxT("Hysteresis"));
    txtEd_Hysteresis = new wxTextCtrl(notebook_1_pane_1, -1, wxT(""));
    label_17 = new wxStaticText(notebook_1_pane_1, -1, wxT("Bias"));
    txtEd_Bias = new wxTextCtrl(notebook_1_pane_1, -1, wxT(""));
    label_1 = new wxStaticText(notebook_1_pane_1, -1, wxT("Input Property:"));
    button_invert_input = new wxToggleButton(notebook_1_pane_1, -1, wxT("+"));
    txtEd_InputProperty = new wxTextCtrl(notebook_1_pane_1, -1, wxT(""));
    label_output_property = new wxStaticText(notebook_1_pane_1, -1, wxT("Output Property:"));
    txtEd_OutputProperty = new wxTextCtrl(notebook_1_pane_1, -1, wxT(""));
    txtEd_Description = new wxTextCtrl(nbk_Properties, -1, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER|wxTE_PROCESS_TAB|wxTE_MULTILINE|wxTE_WORDWRAP);
    button_OK = new wxButton(this, wxID_OK, wxT("OK"));
    button_Cancel = new wxButton(this, wxID_CANCEL, wxT("Cancel"));
    button_Help = new wxButton(this, -1, wxT("Help"));

    set_properties();
    do_layout();
    // end wxGlade
    GetDataIn(actuator);
}


BEGIN_EVENT_TABLE(ActuatorComponentEditor, wxDialog)
    // begin wxGlade: ActuatorComponentEditor::event_table
    EVT_CHECKBOX(-1, ActuatorComponentEditor::OnCheckboxClip)
    EVT_TOGGLEBUTTON(-1, ActuatorComponentEditor::OnClickInvertInput)
    EVT_BUTTON(wxID_OK, ActuatorComponentEditor::OnButtonPressOK)
    EVT_BUTTON(wxID_CANCEL, ActuatorComponentEditor::OnButtonPressCancel)
    EVT_BUTTON(-1, ActuatorComponentEditor::OnButtonPressHelp)
    // end wxGlade
END_EVENT_TABLE();


void ActuatorComponentEditor::OnCheckboxClip(wxCommandEvent &event)
{
    event.Skip();
}


void ActuatorComponentEditor::OnClickInvertInput(wxCommandEvent &event)
{
    event.Skip();
}


void ActuatorComponentEditor::OnButtonPressOK(wxCommandEvent &event)
{
    if (nbk_Properties->Validate()) nbk_Properties->TransferDataFromWindow();

    event.Skip();
}


void ActuatorComponentEditor::OnButtonPressCancel(wxCommandEvent &event)
{
    event.Skip();
}


void ActuatorComponentEditor::OnButtonPressHelp(wxCommandEvent &event)
{
    event.Skip();
}


// wxGlade: add ActuatorComponentEditor event handlers


void ActuatorComponentEditor::set_properties()
{
    // begin wxGlade: ActuatorComponentEditor::set_properties
    SetTitle(wxT("Actuator Component Editor"));
    txtEd_Name->SetToolTip(wxT("This is the name of the component. It can be a property name, or a human readable name, such as \"Pitch Feedback PID Control\". In the latter case, the name will be converted internally to a property name. In the given example, this will be \"fcs/pitch-feedback-pid-control\"."));
    txtEd_Name->SetFocus();
    txtEd_Type->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INACTIVECAPTION));
    txtEd_Order->SetToolTip(wxT("This number represents the order in which the component will be \"executed\"."));
    label_clip_min->SetMinSize(wxSize(60, -1));
    txtEd_MinClip->SetMinSize(wxSize(100,-1));
    txtEd_MinClip->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INACTIVECAPTION));
    txtEd_MinClip->SetToolTip(wxT("This is the minimum value allowed for the component output. "));
    label_clip_max->SetMinSize(wxSize(60, -1));
    txtEd_MaxClip->SetMinSize(wxSize(100,-1));
    txtEd_MaxClip->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INACTIVECAPTION));
    txtEd_MaxClip->SetToolTip(wxT("This is the maximum value allowed for the component output. "));
    button_invert_input->SetMinSize(wxSize(21, 21));
    button_invert_input->SetToolTip(wxT("Depress this button to invert the input property."));
    button_invert_input->Enable(false);
    txtEd_InputProperty->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INACTIVECAPTION));
    txtEd_InputProperty->SetToolTip(wxT("This is the input property to the component. This is selected via connecting up the components in the editor."));
    txtEd_OutputProperty->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INACTIVECAPTION));
    txtEd_OutputProperty->SetToolTip(wxT("This is the output property to the component. This is selected via connecting up the components in the editor."));
    button_OK->SetToolTip(wxT("Clicking this button will save the changes made."));
    button_Cancel->SetToolTip(wxT("Clicking on the Cancel button will abandon changes made."));
    // end wxGlade
}


void ActuatorComponentEditor::do_layout()
{
    // begin wxGlade: ActuatorComponentEditor::do_layout
    wxFlexGridSizer* grid_sizer_toplevel = new wxFlexGridSizer(2, 1, 5, 0);
    wxBoxSizer* sizer_buttons = new wxBoxSizer(wxHORIZONTAL);
    wxFlexGridSizer* grid_sizer_2 = new wxFlexGridSizer(5, 1, 5, 0);
    wxStaticBoxSizer* sizer_inout = new wxStaticBoxSizer(sizer_inout_staticbox, wxHORIZONTAL);
    wxFlexGridSizer* grid_sizer_inout = new wxFlexGridSizer(2, 2, 5, 5);
    wxFlexGridSizer* grid_sizer_1 = new wxFlexGridSizer(1, 2, 0, 2);
    wxStaticBoxSizer* sizer_actuator_properties = new wxStaticBoxSizer(sizer_actuator_properties_staticbox, wxVERTICAL);
    wxGridSizer* grid_sizer_7 = new wxGridSizer(4, 2, 3, 3);
    wxStaticBoxSizer* sizer_clip = new wxStaticBoxSizer(sizer_clip_staticbox, wxHORIZONTAL);
    wxFlexGridSizer* grid_sizer_clip = new wxFlexGridSizer(2, 2, 5, 10);
    wxBoxSizer* sizer_clip_min = new wxBoxSizer(wxHORIZONTAL);
    wxStaticBoxSizer* sizer_basic = new wxStaticBoxSizer(sizer_basic_staticbox, wxHORIZONTAL);
    wxFlexGridSizer* grid_sizer_basic_interior = new wxFlexGridSizer(3, 2, 5, 5);
    grid_sizer_basic_interior->Add(label_name, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 0);
    grid_sizer_basic_interior->Add(txtEd_Name, 0, wxEXPAND|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 0);
    grid_sizer_basic_interior->Add(label_type, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 0);
    grid_sizer_basic_interior->Add(txtEd_Type, 0, wxEXPAND|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 0);
    grid_sizer_basic_interior->Add(label_order, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 0);
    grid_sizer_basic_interior->Add(txtEd_Order, 0, wxEXPAND|wxALIGN_CENTER_VERTICAL|wxFIXED_MINSIZE, 0);
    grid_sizer_basic_interior->AddGrowableCol(1);
    sizer_basic->Add(grid_sizer_basic_interior, 1, wxEXPAND|wxADJUST_MINSIZE, 0);
    grid_sizer_2->Add(sizer_basic, 1, wxEXPAND|wxADJUST_MINSIZE, 0);
    grid_sizer_clip->Add(chx_Clip, 0, wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 0);
    sizer_clip_min->Add(label_clip_min, 0, wxLEFT|wxRIGHT|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_clip_min->Add(txtEd_MinClip, 0, wxEXPAND|wxALIGN_CENTER_VERTICAL|wxFIXED_MINSIZE, 0);
    sizer_clip_min->Add(label_clip_max, 0, wxLEFT|wxRIGHT|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_clip_min->Add(txtEd_MaxClip, 0, wxEXPAND|wxALIGN_CENTER_VERTICAL|wxFIXED_MINSIZE, 0);
    grid_sizer_clip->Add(sizer_clip_min, 1, wxEXPAND|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 0);
    grid_sizer_clip->AddGrowableCol(1);
    sizer_clip->Add(grid_sizer_clip, 1, wxEXPAND|wxADJUST_MINSIZE, 0);
    grid_sizer_2->Add(sizer_clip, 1, wxEXPAND|wxADJUST_MINSIZE, 0);
    grid_sizer_7->Add(label_7, 0, wxADJUST_MINSIZE, 0);
    grid_sizer_7->Add(txtEd_Lag, 0, wxADJUST_MINSIZE, 0);
    grid_sizer_7->Add(label_10, 0, wxADJUST_MINSIZE, 0);
    grid_sizer_7->Add(txtEd_RateLimit, 0, wxADJUST_MINSIZE, 0);
    grid_sizer_7->Add(label_11, 0, wxADJUST_MINSIZE, 0);
    grid_sizer_7->Add(txtEd_Hysteresis, 0, wxADJUST_MINSIZE, 0);
    grid_sizer_7->Add(label_17, 0, wxADJUST_MINSIZE, 0);
    grid_sizer_7->Add(txtEd_Bias, 0, wxADJUST_MINSIZE, 0);
    sizer_actuator_properties->Add(grid_sizer_7, 1, wxEXPAND, 0);
    grid_sizer_2->Add(sizer_actuator_properties, 1, wxEXPAND|wxADJUST_MINSIZE, 0);
    grid_sizer_inout->Add(label_1, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 0);
    grid_sizer_1->Add(button_invert_input, 0, wxADJUST_MINSIZE, 0);
    grid_sizer_1->Add(txtEd_InputProperty, 0, wxEXPAND|wxADJUST_MINSIZE, 0);
    grid_sizer_1->AddGrowableCol(1);
    grid_sizer_inout->Add(grid_sizer_1, 1, wxEXPAND, 0);
    grid_sizer_inout->Add(label_output_property, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 0);
    grid_sizer_inout->Add(txtEd_OutputProperty, 0, wxEXPAND|wxADJUST_MINSIZE, 0);
    grid_sizer_inout->AddGrowableCol(1);
    sizer_inout->Add(grid_sizer_inout, 1, wxEXPAND, 0);
    grid_sizer_2->Add(sizer_inout, 1, wxEXPAND, 0);
    notebook_1_pane_1->SetAutoLayout(true);
    notebook_1_pane_1->SetSizer(grid_sizer_2);
    grid_sizer_2->Fit(notebook_1_pane_1);
    grid_sizer_2->SetSizeHints(notebook_1_pane_1);
    grid_sizer_2->AddGrowableCol(0);
    nbk_Properties->AddPage(notebook_1_pane_1, wxT("Properties"));
    nbk_Properties->AddPage(txtEd_Description, wxT("Comments"));
    grid_sizer_toplevel->Add(nbk_Properties, 1, wxEXPAND, 0);
    sizer_buttons->Add(button_OK, 0, wxLEFT|wxRIGHT|wxADJUST_MINSIZE, 5);
    sizer_buttons->Add(button_Cancel, 0, wxLEFT|wxRIGHT, 5);
    sizer_buttons->Add(button_Help, 0, wxLEFT|wxRIGHT|wxADJUST_MINSIZE, 5);
    grid_sizer_toplevel->Add(sizer_buttons, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 5);
    SetAutoLayout(true);
    SetSizer(grid_sizer_toplevel);
    grid_sizer_toplevel->Fit(this);
    grid_sizer_toplevel->SetSizeHints(this);
    grid_sizer_toplevel->AddGrowableRow(0);
    grid_sizer_toplevel->AddGrowableCol(0);
    Layout();
    Centre();
    // end wxGlade
}

/**
* Show =========================================================================
*/

bool ActuatorComponentEditor::Show( bool show)
{
  bool tmp = wxDialog::Show(show);

  if (show) nbk_Properties->InitDialog();

  return tmp;
}

/**
* GetDataIn ====================================================================
*/

void ActuatorComponentEditor::GetDataIn(Actuator * g)
{
  //name     = g->GetName();
  *txtEd_Name << g->GetName();
  description  = g->GetDescription();
  order    = wxString::Format(wxT("%ld"), g->GetOrder());
  clipable = g->IsClipable();
  clip_max = g->GetClipMax();
  clip_min = g->GetClipMin();

  *txtEd_Order << order;

  if (clipable) {
      txtEd_MaxClip->Enable(true);
      txtEd_MaxClip->SetBackgroundColour(*wxWHITE);
      txtEd_MinClip->Enable(true);
      txtEd_MinClip->SetBackgroundColour(*wxWHITE);
  } else {
      txtEd_MaxClip->Enable(false);
      txtEd_MaxClip->SetBackgroundColour(wxSYS_COLOUR_INACTIVECAPTION);
      txtEd_MinClip->Enable(false);
      txtEd_MinClip->SetBackgroundColour(wxSYS_COLOUR_INACTIVECAPTION);
  }

  wxArrayString inputs = g->GetInputNames();

  txtEd_OutputProperty->SetValue(g->GetOutputName());

  txtEd_Lag->SetValue(wxT(""));        *txtEd_Lag << g->GetLag();
  txtEd_RateLimit->SetValue(wxT(""));  *txtEd_RateLimit << g->GetRate_Limit();
  txtEd_Hysteresis->SetValue(wxT("")); *txtEd_Hysteresis << g->GetHysteresis_Width();
  txtEd_Bias->SetValue(wxT(""));       *txtEd_Bias << g->GetBias();

  if (inputs.GetCount() > 0) {
    if (inputs[0] != wxT("NULL"))
    {
      button_invert_input->Enable();
      if (g->GetInputIsInverted()) {
        button_invert_input->SetValue(true);
        button_invert_input->SetLabel(wxT("-"));
      } else {
        button_invert_input->SetValue(false);
        button_invert_input->SetLabel(wxT("+"));
      }
    }
    txtEd_InputProperty->SetValue(inputs[0]);
  }
  txtEd_Description->SetValue(g->GetDescription());

}

/**
* SetDataOut ===================================================================
*/

void ActuatorComponentEditor::SetDataOut(Actuator * g)
{
  g->SetName(txtEd_Name->GetValue());

  long int tmpl;
  double tmp;

  order.ToLong(&tmpl);
  g->SetOrder(tmpl);
  g->SetClipable(clipable);
  double tmpd;
  g->SetClipMax(clip_max);
  g->SetClipMin(clip_min);

  g->SetDescription(txtEd_Description->GetValue());

  txtEd_Lag->GetValue().ToDouble(&tmp);
  g->SetLag(tmp);

  txtEd_RateLimit->GetValue().ToDouble(&tmp);
  g->SetRate_Limit(tmp);

  txtEd_Hysteresis->GetValue().ToDouble(&tmp);
  g->SetHysteresis_Width(tmp);

  txtEd_Bias->GetValue().ToDouble(&tmp);
  g->SetBias(tmp);

  g->SetInputIsInverted(false);
  if (txtEd_InputProperty->GetValue() != wxT("NULL")) // true if input prop is present
    if (button_invert_input->GetValue()) // true if inverted
      g->SetInputIsInverted(true);

}
